/* Y o u r   D e s c r i p t i o n                       */
/*                            AppBuilder Photon Code Lib */
/*                                         Version 2.01  */

/* Standard headers */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>

#include <math.h>

/* Local headers */
#include "ablibs.h"
#include "abimport.h"
#include "proto.h"
#include "../api.h"
#include "../api/memtype.h"


int
TIM_activ( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
	short 				state;
	int					*addr;
	int					fd;	
	short				*selected;
	mem_data_t			data;	
	char 				str [255];
	static double		x;
	
	/* eliminate 'unreferenced' warnings */
	widget = widget, apinfo = apinfo, cbinfo = cbinfo;

	PtGetResource (ABW_Num1, Pt_ARG_NUMERIC_VALUE, &addr, 0);
	PtGetResource (ABW_Combo1, Pt_ARG_CBOX_SEL_ITEM, &selected, 0);
		
	fd = open("/dev/sfins", O_RDONLY);
	if (fd == -1) {
		perror(NULL);
		return (-1);
	}
	
	switch (*selected) {
		case 1:	 data.code   = CIO_WRD; break;
		case 2:	 data.code   = WR_WRD;  break;
		case 3:	 data.code   = DM_WRD;  break;
		case 4:	 data.code   = TIM_WRD; break;
		case 5:	 data.code   = CNT_WRD; break;
		default: data.code   = CIO_WRD; break;
	}
	data.n_elem	 = 1;
	data.addr = (*addr) << 8;
	// если запрашивается счетчик, то к его адресу нужно добавить 0x800000
	if (data.code == CNT_WRD) data.addr |= 0x800000;
	devctl(fd, DCMD_READ_MEM, &data, sizeof (data), NULL); 
	state = data.data [0];

	sprintf (str, "#%04hX (&%05hd)", state, state);
	PtSetResource (ABW_Label, Pt_ARG_TEXT_STRING, str, 0);

	PtSetResource (ABW_R0,  Pt_ARG_FILL_COLOR, (!!((state >>  0)&1))? Pg_RED: Pg_GREEN, 0 );
	PtSetResource (ABW_R1,  Pt_ARG_FILL_COLOR, (!!((state >>  1)&1))? Pg_RED: Pg_GREEN, 0 );
	PtSetResource (ABW_R2,  Pt_ARG_FILL_COLOR, (!!((state >>  2)&1))? Pg_RED: Pg_GREEN, 0 );
	PtSetResource (ABW_R3,  Pt_ARG_FILL_COLOR, (!!((state >>  3)&1))? Pg_RED: Pg_GREEN, 0 );
	PtSetResource (ABW_R4,  Pt_ARG_FILL_COLOR, (!!((state >>  4)&1))? Pg_RED: Pg_GREEN, 0 );
	PtSetResource (ABW_R5,  Pt_ARG_FILL_COLOR, (!!((state >>  5)&1))? Pg_RED: Pg_GREEN, 0 );
	PtSetResource (ABW_R6,  Pt_ARG_FILL_COLOR, (!!((state >>  6)&1))? Pg_RED: Pg_GREEN, 0 );
	PtSetResource (ABW_R7,  Pt_ARG_FILL_COLOR, (!!((state >>  7)&1))? Pg_RED: Pg_GREEN, 0 );
	PtSetResource (ABW_R8,  Pt_ARG_FILL_COLOR, (!!((state >>  8)&1))? Pg_RED: Pg_GREEN, 0 );
	PtSetResource (ABW_R9,  Pt_ARG_FILL_COLOR, (!!((state >>  9)&1))? Pg_RED: Pg_GREEN, 0 );
	PtSetResource (ABW_R10, Pt_ARG_FILL_COLOR, (!!((state >> 10)&1))? Pg_RED: Pg_GREEN, 0 );
	PtSetResource (ABW_R11, Pt_ARG_FILL_COLOR, (!!((state >> 11)&1))? Pg_RED: Pg_GREEN, 0 );
	PtSetResource (ABW_R12, Pt_ARG_FILL_COLOR, (!!((state >> 12)&1))? Pg_RED: Pg_GREEN, 0 );
	PtSetResource (ABW_R13, Pt_ARG_FILL_COLOR, (!!((state >> 13)&1))? Pg_RED: Pg_GREEN, 0 );
	PtSetResource (ABW_R14, Pt_ARG_FILL_COLOR, (!!((state >> 14)&1))? Pg_RED: Pg_GREEN, 0 );
	PtSetResource (ABW_R15, Pt_ARG_FILL_COLOR, (!!((state >> 15)&1))? Pg_RED: Pg_GREEN, 0 );

	PtSetResource (ABW_btn0,  Pt_ARG_ONOFF_STATE, (state >>  0)&1, 0);
	PtSetResource (ABW_btn1,  Pt_ARG_ONOFF_STATE, (state >>  1)&1, 0);
	PtSetResource (ABW_btn2,  Pt_ARG_ONOFF_STATE, (state >>  2)&1, 0);
	PtSetResource (ABW_btn3,  Pt_ARG_ONOFF_STATE, (state >>  3)&1, 0);
	PtSetResource (ABW_btn4,  Pt_ARG_ONOFF_STATE, (state >>  4)&1, 0);
	PtSetResource (ABW_btn5,  Pt_ARG_ONOFF_STATE, (state >>  5)&1, 0);
	PtSetResource (ABW_btn6,  Pt_ARG_ONOFF_STATE, (state >>  6)&1, 0);
	PtSetResource (ABW_btn7,  Pt_ARG_ONOFF_STATE, (state >>  7)&1, 0);
	PtSetResource (ABW_btn8,  Pt_ARG_ONOFF_STATE, (state >>  8)&1, 0);
	PtSetResource (ABW_btn9,  Pt_ARG_ONOFF_STATE, (state >>  9)&1, 0);
	PtSetResource (ABW_btn10, Pt_ARG_ONOFF_STATE, (state >> 10)&1, 0);
	PtSetResource (ABW_btn11, Pt_ARG_ONOFF_STATE, (state >> 11)&1, 0);
	PtSetResource (ABW_btn12, Pt_ARG_ONOFF_STATE, (state >> 12)&1, 0);
	PtSetResource (ABW_btn13, Pt_ARG_ONOFF_STATE, (state >> 13)&1, 0);
	PtSetResource (ABW_btn14, Pt_ARG_ONOFF_STATE, (state >> 14)&1, 0);
	PtSetResource (ABW_btn15, Pt_ARG_ONOFF_STATE, (state >> 15)&1, 0);

	close (fd);	

	fd = open("/dev/ctansi", O_RDONLY);
	if (fd == -1) {
		perror("ctansi");
		return (-1);
	}

	x += 0.003;

	data.node_id = 14;
	data.addr = 117;
	data.rdata = 200 * fabs (sin (x));
	devctl(fd, DCMD_WRITE_PAR, &data, sizeof (data), NULL); 
	data.addr = 502;
	data.rdata = 350 * fabs (cos (x));
	devctl(fd, DCMD_WRITE_PAR, &data, sizeof (data), NULL); 
	data.addr = 304;
	data.rdata = 600 * fabs (cos (x+0.5));
	devctl(fd, DCMD_WRITE_PAR, &data, sizeof (data), NULL);
	data.addr = 303;
	data.rdata = 100 * fabs (cos (x+1));
	devctl(fd, DCMD_WRITE_PAR, &data, sizeof (data), NULL);  
	data.node_id = 13;
	data.addr = 502;
	data.rdata = 1000 * fabs (cos (2.3*x));
	devctl(fd, DCMD_WRITE_PAR, &data, sizeof (data), NULL); 


	data.node_id = 24;
	data.addr = 117;
	data.rdata = 200 * fabs (sin (x*4));
	devctl(fd, DCMD_WRITE_PAR, &data, sizeof (data), NULL); 
	data.addr = 502;
	data.rdata = 300 * fabs (cos (x/3));
	devctl(fd, DCMD_WRITE_PAR, &data, sizeof (data), NULL); 
	data.addr = 304;
	data.rdata = 600 * fabs (cos (x*6));
	devctl(fd, DCMD_WRITE_PAR, &data, sizeof (data), NULL); 
	data.addr = 303;
	data.rdata = 100 * fabs (cos (x-1));
	devctl(fd, DCMD_WRITE_PAR, &data, sizeof (data), NULL);  
	data.node_id = 23;
	data.addr = 502;
	data.rdata = 1000 * fabs (cos (2.3*x));
	devctl(fd, DCMD_WRITE_PAR, &data, sizeof (data), NULL); 


	close (fd);

	return( Pt_CONTINUE );
}

